local condition = require "astronvim.utils.status.condition"
local hl = require "astronvim.utils.status.hl"
local status_utils = require "astronvim.utils.status.utils"

local utils = require "astronvim.utils"
local is_available = utils.is_available

local opts_d = {
  ERROR = { icon = { kind = "DiagnosticError", padding = { left = 1, right = 1 } } },
  WARN = { icon = { kind = "DiagnosticWarn", padding = { left = 1, right = 1 } } },
  INFO = { icon = { kind = "DiagnosticInfo", padding = { left = 1, right = 1 } } },
  HINT = { icon = { kind = "DiagnosticHint", padding = { left = 1, right = 1 } } },
  surround = { separator = "left", color = "diagnostics_bg", condition = condition.has_diagnostics },
  hl = hl.get_attributes "diagnostics",
  on_click = {
    name = "heirline_diagnostic",
    callback = function()
      if is_available "telescope.nvim" then
        vim.defer_fn(function() require("telescope.builtin").diagnostics() end, 100)
      end
    end,
  },
  update = { "DiagnosticChanged", "BufEnter" },
}

return {
  "rebelot/heirline.nvim",
  opts = function(_, opts)
    local status = require "astronvim.utils.status"
    opts.statusline[1] = status.component.mode {
      mode_text = { padding = { left = 1, right = 1 } },
      hl = { fg = "#111111", bold = true },
    }
    opts.statusline[5] =
        status.component.builder(status_utils.setup_providers(opts_d, { "ERROR", "WARN", "INFO" }, function(p_opts, p)
          local out = status_utils.build_provider(p_opts, p)
          if out then
            out.provider = "diagnostics"
            out.opts.severity = p
            if out.hl == nil then out.hl = { fg = "diag_" .. p } end
          end
          return out
        end))
  end,
}
